package com.abocode.aichat.server.context;

import com.abocode.aichat.server.exception.ServerContextException;
import com.abocode.aichat.server.exception.ServerContextException;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by franky-8.
 */
public abstract class AbstractServerContext<T extends ServerContextName, K extends ServerContextName> implements ServerContext<T, K> {

    private Map<String, T> context = new HashMap<String, T>();

    @Override
    public T get(K k) {
        if (isContains(k.name())) {
            return context.get(k.name());
        }
        throw new ServerContextException(k.name() + " not register");
    }

    @Override
    public void register(T t) {
        if (!isContains(t.name())) {
            context.put(t.name(), t);
            return;
        }
        throw new ServerContextException(t.getClass().getName() + "register");
    }

    public boolean isContains(String key) {
        boolean flag = context.containsKey(key);
        return flag;
    }

    @Override
    public void unregister(T t) {
        if (isContains(t.name())) {
            context.remove(t.name());
            return;
        }
        throw new ServerContextException(t.name() + " not register");
    }

}
